package main

import (
	"changeme/packge/global"
	"changeme/serve"
	_ "embed"
	"encoding/json"
	"fmt"
	"os"

	"github.com/wailsapp/wails/v2/pkg/options"

	"context"
)

//go:embed Data/Tool/FmTool.exe
var Fmtool []byte

//go:embed Data/Tool/HPSocket_U.dll
var HPSocket_U []byte

//go:embed Data/Tool/libcrypto-1_1-x64.dll
var libcrypto []byte

//go:embed Data/Tool/libssl-1_1-x64.dll
var libssl []byte

// App struct
type App struct {
	ctx context.Context
}

// NewApp creates a new App application struct
func NewApp() *App {
	return &App{}
}

// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
	a.ctx = ctx
	global.Ctx = ctx
	//创建文件夹

	//创建数据目录
	os.Mkdir("Data", os.ModePerm)
	//创建本地数据库目录
	os.Mkdir("Data/Database", os.ModePerm)
	//创建游戏目录
	os.Mkdir("Data/Game", os.ModePerm)
	//创建插件目录
	os.Mkdir("Data/Plugin", os.ModePerm)
	//创建工具目录
	os.Mkdir("Data/Tool", os.ModePerm)

	os.WriteFile("Data/Tool/FmTool.exe", Fmtool, 0644)
	os.WriteFile("Data/Tool/HPSocket_U.dll", HPSocket_U, 0644)
	os.WriteFile("Data/Tool/libcrypto-1_1-x64.dll", libcrypto, 0644)
	os.WriteFile("Data/Tool/libssl-1_1-x64.dll", libssl, 0644)

	//启动弹幕服务器
	go serve.RunServe()
	//检查更新
	//api.GetVersion()
	//添加窗口托盘
	//有BUG先不添加了
	//go tray.RunTray()

}

func (a *App) onSecondInstanceLaunch(data options.SecondInstanceData) {
	marshal, err := json.Marshal(data)
	if err != nil {
		return
	}

	fmt.Println(string(marshal))
}
